package com.itmg.search.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.itmg.factory.ServiceFactory;
import com.itmg.search.business.service.SearchService;
import com.itmg.search.form.SearchForm;
import com.itmg.search.web.ResultView;
import com.itmg.tools.page.PageBean;
import com.itmg.tools.page.PageHandler;
import com.itmg.tools.thread.ShopHistoryThread;
import com.itmg.util.Constants;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class SearchAction extends ActionSupport implements ModelDriven<SearchForm>{

	private static final long serialVersionUID = 1L;
	
	private SearchService searchService = ServiceFactory.getSearchService();
	private SearchForm searchForm;
	private ResultView resultView;
	private Integer index;
	private List<String> historyList;
	private Integer count;
	
	public Integer getIndex() {
		return index;
	}
	public void setIndex(Integer index) {
		this.index = index;
	}
	public ResultView getResultView() {
		return resultView;
	}
	public void setResultView(ResultView resultView) {
		this.resultView = resultView;
	}

	public List<String> getHistoryList() {
		return historyList;
	}
	public void setHistoryList(List<String> historyList) {
		this.historyList = historyList;
	}
	
	public Integer getCount() {
		return count;
	}
	public void setCount(Integer count) {
		this.count = count;
	}
	
	//查询方法
	public String search() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest ();	
		HttpServletResponse response = ServletActionContext.getResponse();
		if(searchForm.getKeywords() == null || "".trim().equals(searchForm.getKeywords()))
			return INPUT;
		
		//判断是否结束转向
		String forwardURL = searchService.getStaticURL(searchForm,request);
		if(forwardURL != null && !"".equals(forwardURL)){
			response.sendRedirect(forwardURL);
			return null;
		}
		
		ShopHistoryThread shopHistoryThread = new ShopHistoryThread(request,response);
		Thread thread = new Thread(shopHistoryThread);
		thread.start();
		
		//分页信息
		PageBean page = new PageBean();
		if (request.getParameter(Constants.PART_PAGE_PAGESEARCH) != null && request.getParameter(Constants.PART_PAGE_PAGESEARCH).equals("true")) {
			page = PageHandler.initPage(request);
		}
		page.setLength(Constants.PART_PAGE_PER_COUNT_10);
		if (index != null) {
			page.setGotoPage(index);
		} else {
			page.setGotoPage(1);
		}
		
        resultView = searchService.getResultView(searchForm, page,request);
        thread.join();
        historyList = shopHistoryThread.getHistoryURLList();
        if(historyList.size() == 0)
        	count = 0;
        else
        	count = 1;
		//把分页信息传入页面处理类
        PageHandler.putPage(request,page);
		return SUCCESS;
	}
	
	public SearchForm getModel() {
		searchForm = new SearchForm();
		return searchForm;
	}

}
